/*
 * Copyright 2011 John Alström
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package se.elva.slticket;

import se.elva.slticket.dialog.ConfirmDialog;
import se.elva.slticket.dialog.ConfirmPasswordDialog;
import se.elva.slticket.dialog.SettingsDialog;
import se.elva.slticket.repository.SavedStates;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public abstract class SmsTicket extends Activity{

	/** Called when the activity is first created. */
	private final int MENUITEM_ABOUT = 0;
	private final int MENUITEM_QUIT = 2;
	private final int MENUITME_PICKCITY = 3;
	private final int MENUITEM_SETTINGS = 4;
	protected int saveChoice =0; 
	private String TAG ="SmsTicket";
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENUITEM_ABOUT:
			showInfoDialog("Info",
					getString(R.string.creator));
			return true;
		case MENUITME_PICKCITY:
			showDialog(0);
			return true;
		case MENUITEM_QUIT:
			finish();
			return true;
		case MENUITEM_SETTINGS:
			showSettingsDialog();
			return true;
		}
		return false;
	}
	protected String getPrefix() {
		return this.getSharedPreferences("SLTicket", 0).getString("prefix", "");
	}
	
	protected void showSettingsDialog() {
		final SettingsDialog info = new SettingsDialog(this);
		info.show();
	}
	protected abstract void showInfoDialog(String title, String msg);
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem itemAbout = menu.add(0, MENUITEM_ABOUT, 0, R.string.about);
		itemAbout.setIcon(android.R.drawable.ic_menu_info_details);
		
		MenuItem itemSettings = menu.add(0, MENUITEM_SETTINGS, 0,"Inställningar");
		itemSettings.setIcon(android.R.drawable.ic_menu_edit);
		
		
		MenuItem itemOstgota = menu.add(0, MENUITME_PICKCITY, 0, R.string.pickCity);
		itemOstgota.setIcon(android.R.drawable.ic_menu_gallery);
		
		
		return true;
	}
	
	protected Intent findIntentToStart(Activity act,int saveChoice) {
		return findIntentToStart(act, saveChoice,true);	
	}	
	
	protected Intent findIntentToStart(Activity act,int saveChoice,boolean notSlTicket) {
		if (saveChoice == 0) {
			return new Intent(act.getApplicationContext(),
					HallandsTrafiken.class);
		} else if (saveChoice == 1) {
			return new Intent(act.getApplicationContext(),
					LanstrafikenSormland.class);
		} else if (saveChoice == 2) {
			return new Intent(act.getApplicationContext(), Orebro.class);
		} else if (saveChoice == 3 && notSlTicket) {
			return new Intent(act.getApplicationContext(), SLTicket.class);
		} else if (saveChoice == 4) {
			return new Intent(act.getApplicationContext(), Umea.class);
		} else if (saveChoice == 5) {
			return new Intent(act.getApplicationContext(), UppsalaTabActivity.class);
		} else if (saveChoice == 6) {
			return new Intent(act.getApplicationContext(),
					VasterasLansTrafik.class);
		} else if (saveChoice == 7) {
			return new Intent(act.getApplicationContext(), VastTrafiken.class);
		} else if (saveChoice == 8) {
			return new Intent(act.getApplicationContext(),
					OstgotaTrafiken.class);
		}
		return null;
	}
	
	protected void setCodeToSend(String priceCategory,String city,String phoneNumber) {
		TextView codeView = (TextView) findViewById(R.id.code);
		String value = getString(R.string.smsInformationGenrell);
		value = value.replace(":smsCode", city+priceCategory);
		value = value.replace(":telefon", phoneNumber);
		codeView.setText(value);
	}
	
	protected void sendSms(String phonenumber, String text,Activity act) {
		if(requireConfirmCheckBox(SettingsDialog.CHECKBOX_PASSWORD, false)){
			showConfirmSendPassword();
		}else if (requireConfirmCheckBox(SettingsDialog.CHECKBOX_CONFIRM, true)) {	
			showConfirmSendDialog();
		}else{
			sendSmsDirect(phonenumber, text);
		}
	}
	
	private void showConfirmSendPassword() {
		final ConfirmPasswordDialog confirm = new ConfirmPasswordDialog(this);
		confirm.getOkButton().setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (confirm.getPassword().equals(new SavedStates(getApplicationContext()).getSavedPassword())) {
					sendSmsDirect(getPhoneNumer(), getTextToSend());
					confirm.cancel();
				}
			}
		});
		
		confirm.show();
		
	}


	private void showConfirmSendDialog() {
		final ConfirmDialog confirm = new ConfirmDialog(this, "Bekräfta köp\n" + getTextToSend() + " till " + getPhoneNumer());
		confirm.getOkButton().setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				sendSmsDirect(getPhoneNumer(), getTextToSend());
				confirm.cancel();
			}
		});
		
		confirm.show();
		
	}
	
	public abstract String getPhoneNumer();
	public abstract String getTextToSend();

	/**
	 * Should change to the new one(but the tatto bug)
	 * @param phonenumber
	 * @param text
	 */
	private void sendSmsDirect(String phonenumber, String text){
		SmsManager sms = SmsManager.getDefault();

		sms.sendTextMessage(phonenumber, null, text, null, null);
		Log.d(TAG, "Sending message");
		Toast.makeText(this.getApplicationContext(), R.string.smsSend,
				Toast.LENGTH_LONG).show();
		finish();
	}
	
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
	// = listeners
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

	OnClickListener mCancelListener = new OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};
	
	
	private boolean requireConfirmCheckBox(String key,boolean def){
		SharedPreferences pref = getApplicationContext().getSharedPreferences("SLTicket", 0);
        return pref.getBoolean(key, def);
	}
    /**
     * Any time we are paused we need to save away the current state, so it
     * will be restored correctly when we are resumed.
     */
    @Override
    protected void onPause() {
        super.onPause();

        SharedPreferences.Editor editor = getSharedPreferences("SLTicket", 0).edit();
        editor.putString("saveChoice", String.valueOf(saveChoice));
        editor.commit();
    }
	
}
